#include "EMushroom.h"
#include "D3DApp.h"
#include "Collision.h"
#include "ObPlayer.h"
#include "ObBullet.h"
#include "ObBrick.h"
#include "ObBrickSP.h"
#include "ObDeathField.h"

IDirect3DTexture9* EMushRoom::mTex = 0;
CSound* EMushRoom::sDie				= 0;
EMushRoom::EMushRoom(){
	setId(MOB01);
	setInfo(RIGHT);
}

EMushRoom::EMushRoom(ENERMY2 nid, INFO ninfo){
	setId(nid);
	setInfo(ninfo);
}

EMushRoom::~EMushRoom(){

}

bool EMushRoom::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 16.0f;
		mHeight = 16.0f;
		mCenter = D3DXVECTOR3(mWidth/2,mHeight/2,0.0f);

		mSpritePos.x = 10;
		mSpritePos.y = 10;

		MAX_VELOCITYX	= DEFAULTSPEED_X;
		MAX_VELOCITYY	= 0.0f;
		mVelocity		= D3DXVECTOR3(-DEFAULTSPEED_X,0.0f,0.0f);
		mIsYGravity = true;
		mIsXGravity = false;

		mIsTurnAround	= true;
		mCurrentSprite	= 0;
		mCurrentAction	= RUN;

		count = 0.0f;
		delaycount = 0.0f;

		switch (nid)
		{
		case ObEnemies::MOB01:
			break;
		case ObEnemies::MOB02:
			mSpritePos.y+=20;
			break;
		case ObEnemies::MOB03:
			mSpritePos.y+=40;
			break;
		default:
			break;
		}
		return true;
	}else{
		return false;
	}
}

bool EMushRoom::setInfo(int ninfo){
	if(checkInfo(ninfo)){
		mInfo = ninfo;
		if(ninfo == RIGHT){
			mRightFaced = RIGHT;
			mVelocity.x = DEFAULTSPEED_X;
		}else{
			mRightFaced = LEFT;
			mVelocity.x = -DEFAULTSPEED_X;
		}
		return true;
	}else{
		return false;
	}
}

bool EMushRoom::checkType(int nid){
	if(nid<MOB01 || nid>MOB03){
		return false;
	}
	return true;
}

bool EMushRoom::checkInfo(int ninfo){
	if(ninfo<LEFT || ninfo>RIGHT){
		return false;
	}
	return true;
}

void EMushRoom::collidedWith(ObObject* object, CollisionRS rs) {

	switch (object->getId())
	{
	case ObPlayer::MARIO:
		if(ObPlayer* player  = dynamic_cast<ObPlayer*>(object)){
			if(player->getIsGoD()){
				PlaySound(sDie);
				mCurrentAction = DIE;
			}
			if(rs.mState == Top){
				PlaySound(sDie);
				mCurrentAction = DIE;
			}
		}
		break;
	case ObBullet::BL1:
		if(ObBullet* player  = dynamic_cast<ObBullet*>(object)){
			PlaySound(sDie);
			mCurrentAction = DIE;
		}
		break;
	case ObDeathField::Deathfield1:
		mNeedDispose = true;
		break;
	default:
		if(rs.mState == Left){
			mVelocity.x = -mVelocity.x;
			if(mVelocity.x < 0){
				mRightFaced = LEFT;
			}else{
				mRightFaced = RIGHT;
			}
		}
		if(rs.mState == Right){
			mVelocity.x = -mVelocity.x;
			if(mVelocity.x < 0){
				mRightFaced = LEFT;
			}else{
				mRightFaced = RIGHT;
			}
		}
		if(rs.mState == Bottom){
			mVelocity.y = 0.0f;
			mIsJump = false;
		}

		if(ObBrick* brick  = dynamic_cast<ObBrick*>(object)){
			if(brick->getCanKill()){
				PlaySound(sDie);
				mCurrentAction = DIE;
			}
		}else if(ObBrickSP* bricksp  = dynamic_cast<ObBrickSP*>(object)){
			if(bricksp->getCanKill()){
				PlaySound(sDie);
				mCurrentAction = DIE;
			}
		}
		break;
	}
}

void EMushRoom::update(float dt){
	if(mNeedDispose) return;

	switch (mCurrentAction)
	{
	case RUN:
		count+=dt;
		if(count>=0.3f){
			mCurrentSprite++;
			if(mCurrentSprite >= 2){
				mCurrentSprite = 0;
			}
			count = 0.0f;
		}
		mSpritePos.x = 10+mCurrentSprite*20.0f;
		mSpritePos.y = 10;
		break;
	case DIE:
		if(mNeedCollision){
			delaycount = 1.0f;
			mNeedCollision = false;
			mIsYGravity = false;
			mVelocity.x = 0.0f;
			mVelocity.y = 0.0f;
		}
		delaycount-=dt;
		mSpritePos.x = 50;
		mSpritePos.y = 10;
		mCurrentSprite = 0;
		if(delaycount <= 0.0f){
			mNeedDispose = true;
			delaycount = 0.0f;
		}
		break;
	default:
		break;
	}

	int ycal = mID - MOB01;
	mSpritePos.y += 20*ycal;

}

void EMushRoom::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;
	if(mCurrentSprite == -1) return;
	if(mNeedDispose) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x, mPos.y - gCamera->getPos().y, 0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}

bool EMushRoom::loadTexture(){
	sDie = LoadSound("Sounds\\EnemyDie.wav");
	if(mTex == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/MushRoom.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex);
		return true;
	}else{
		return false;
	}
}

bool EMushRoom::releaseTexture(){
	if(sDie != 0){
		delete sDie;
	}

	if(mTex != 0){
		ReleaseCOM(mTex);
		return true;
	}else{
		return false;
	}
}